package com.mc.servlet;

import com.mc.bean.Jury;
import com.mc.service.JuryService;
import com.mc.service.impl.JuryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author fyp
 * 评委修改控制器
 */
@WebServlet("/juryupdate.action")
public class JuryUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建评委逻辑对象
        JuryService juryService=new JuryServiceImpl();
        //接收评委编号
        String jid=req.getParameter("juryid");
        System.out.println(jid);
        //类型转换
        int juryid=Integer.valueOf(jid);
        //接收type
        String type=req.getParameter("type");
        System.out.println(type);
        //通过评委编号查询评委对象
        Jury jury=juryService.findByJuryId(juryid);
        System.out.println(jury);
        if(type.equals("1")){
            //放入作用域
            req.setAttribute("jury",jury);
            //转发
            req.getRequestDispatcher("juryupdate.jsp").forward(req,resp);

        }else if(type.equals("2")){
            String juryname=req.getParameter("juryname");
            String jurytel=req.getParameter("jurytel");
            String jurypassword=req.getParameter("jurypassword");
            String jage=req.getParameter("juryage");
            String jurysex=req.getParameter("jurysex");
            String rids=req.getParameter("rid");
            System.out.println(juryname);
            System.out.println(jurytel);
            //类型转换
            int juryage=Integer.valueOf(jage);
            int rid=Integer.valueOf(rids);
            jury.setJuryname(juryname);
            jury.setJurytel(jurytel);
            jury.setJurypassword(jurypassword);
            jury.setJuryage(juryage);
            jury.setJurysex(jurysex);
            jury.setRid(rid);
            boolean flag=juryService.update(jury);
            System.out.println(flag);
            //重定向
            resp.sendRedirect("jurylist.action");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
